
package com.android.fitnesscenter.view;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.fitnesscenter.R;
import com.android.fitnesscenter.utils.ConfigProperties;
import com.android.fitnesscenter.utils.ConfigProperties.AuditListResult;
import com.android.fitnesscenter.utils.ConfigProperties.QueryDetailResult;
import com.android.fitnesscenter.utils.ConfigProperties.RoleType;
import com.android.fitnesscenter.utils.ConfigProperties.TradingEntity;
import com.android.fitnesscenter.utils.Loger;
import com.android.fitnesscenter.utils.MainApplication;
import com.android.fitnesscenter.utils.MapEntity;
import com.android.fitnesscenter.utils.PhoneStateUtil;
import com.android.fitnesscenter.utils.SharePrefsHelper;
import com.android.fitnesscenter.utils.ToastHelper;
import com.android.fitnesscenter.utils.ViewHelper;
import com.android.fitnesscenter.version.UpgradeProgress;
import com.android.fitnesscenter.view.client.ClientLoginActivity;
import com.android.fitnesscenter.view.server.ServerLoginActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;

public abstract class BaseActivity extends Activity {

    protected SharePrefsHelper mSharePrefsHelper;

    protected ImageButton ibtn_back, ibtn_more;
    protected String[] mUserStrs;

    protected static final int INTENT_SET_WIFI_REQUEST_CODE = 0x0001;

    protected static final int FROM_CHECK_LIST_ACTIVITY = 0x01;
    protected static final int FROM_QUERY_DETAIL_LIST_ACTIVITY = 0x02;
    protected static final int FROM_UNLOCK_ACTIVITY = 0x03;
    protected static final int FROM_AUDIT_LIST_ACTIVITY = 0x04;
    protected static final int FROM_MAIN_ACTIVITY = 0x05;
    protected static final int FROM_SERVER_LOGIN_ACTIVITY = 0x06;
    protected static final int FROM_CLIENT_LOGIN_ACTIVITY = 0x07;
    protected static final int FROM_SERVER_MANGER_ACTIVITY = 0x08;

    protected static final int FROM_CLIENT_ABOUT_ACTIVITY = 0x09;
    protected static final int FROM_SERVER_ABOUT_ACTIVITY = 0x010;

    protected static final int FROM_MODIFY_DETAIL_ACTIVITY = 0x011;

    protected static final int FROM_SERVER_MAIN_TOP_UP_ACTIVITY = 0x012;
    protected static final int FROM_SERVER_MAIN_PAY_ACTIVITY = 0x013;

    protected void initTitleBar(String titleStr) {
        initTitleBar(titleStr, false);
    }

    protected void initTitleBar(String titleStr, boolean isDefault) {
        mSharePrefsHelper = new SharePrefsHelper(ConfigProperties.SHARE_PREFS);
        RelativeLayout include_title_layout = (RelativeLayout) findViewById(R.id.include_title_layout);
        ibtn_back = (ImageButton) include_title_layout.findViewById(R.id.ibtn_back);
        ibtn_more = (ImageButton) include_title_layout.findViewById(R.id.ibtn_more);
        ibtn_back.setOnClickListener(mOnClickListener);
        ibtn_more.setOnClickListener(mOnClickListener);
        TextView txt_title = (TextView) include_title_layout.findViewById(R.id.txt_title);
        if (isDefault) {
            String roleType = mSharePrefsHelper.getString("role_type", RoleType.USER);
            if (TextUtils.equals(roleType, RoleType.MANAGER)) {
                titleStr = getString(R.string.txt_role_manager);
            } else if (TextUtils.equals(roleType, RoleType.OPREATER)) {
                titleStr = getString(R.string.txt_role_operate);
            } else if (TextUtils.equals(roleType, RoleType.USER)) {
                String username = mSharePrefsHelper.getString("username", "");
                if (!TextUtils.isEmpty(username)) {
                    titleStr = username;
                }
            }
        }

        txt_title.setText(titleStr);
    }

    protected boolean isCheckVersion = false;

    protected void setWifiDialog() {
        if (!PhoneStateUtil.isWifi(BaseActivity.this)) {
            DialogInterface.OnClickListener positiveListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (android.os.Build.VERSION.SDK_INT > 10) {
                        // 3.0以上打开设置界面，也可以直接用ACTION_WIRELESS_SETTINGS打开到wifi界面
                        startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS),
                                INTENT_SET_WIFI_REQUEST_CODE);
                    } else {
                        startActivityForResult(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS),
                                INTENT_SET_WIFI_REQUEST_CODE);
                    }
                }
            };
            DialogInterface.OnClickListener negativeListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            };
            DialogInterface.OnCancelListener cancelListener = new DialogInterface.OnCancelListener() {

                @Override
                public void onCancel(DialogInterface dialog) {
                    finish();
                }
            };
            new ViewHelper(BaseActivity.this).showBTN2Dialog("提示", "请打开WIFI后继续使用", "设置", "退出", positiveListener,
                    negativeListener, cancelListener);
        } else {
            if (isCheckVersion) {
                checkNewVersion();
            }
        }
    }

    protected void checkNewVersion() {
        UpgradeProgress upgradeProgress = UpgradeProgress.getInstance();
        upgradeProgress.checkVersion(BaseActivity.this, true, true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == INTENT_SET_WIFI_REQUEST_CODE) {
            setWifiDialog();
        }
    }

    protected String getToken() {
        if (mSharePrefsHelper == null) {
            mSharePrefsHelper = new SharePrefsHelper(ConfigProperties.SHARE_PREFS);
        }
        return mSharePrefsHelper.getString("token", "");
    }

    protected String getIMEI() {
        if (mSharePrefsHelper == null) {
            mSharePrefsHelper = new SharePrefsHelper(ConfigProperties.SHARE_PREFS);
        }
        String imei = mSharePrefsHelper.getString("imei", "");
        if (TextUtils.isEmpty(imei)) {
            TelephonyManager telManager = (TelephonyManager) BaseActivity.this.getSystemService(TELEPHONY_SERVICE);
            imei = telManager.getDeviceId();
            Loger.e("imei is " + imei + " mac is " + imei);
            mSharePrefsHelper.setString("imei", imei);
        }
        return imei;
    }

    private OnClickListener mOnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ibtn_back:
                    ibtn_back();
                    break;
                case R.id.ibtn_more:
                    titlebarMethod(R.id.ibtn_more);
                    break;
            }
        }
    };

    protected void ibtn_back() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Loger.d("pageName:" + getClass().getSimpleName());
        ((MainApplication) MainApplication.getApp()).addActivity(this);
    }

    @Override
    protected void onDestroy() {
        Loger.d("pageName:" + getClass().getSimpleName());
        ((MainApplication) MainApplication.getApp()).removeActivity(this);
        super.onDestroy();
    }

    protected abstract void titlebarMethod(int id);

    protected void backToClientLoginActivity() {
        Intent intent = new Intent(this, ClientLoginActivity.class);
        startActivity(intent);
        ((MainApplication) MainApplication.getApp()).clearChildClientActivitys();
    }

    protected void backToServerLoginActivity() {
        Intent intent = new Intent(this, ServerLoginActivity.class);
        startActivity(intent);
        ((MainApplication) MainApplication.getApp()).clearChildServerActivitys();
    }

    protected DatePicker findDatePicker(ViewGroup group) {
        if (group != null) {
            for (int i = 0, j = group.getChildCount(); i < j; i++) {
                View child = group.getChildAt(i);
                if (child instanceof DatePicker) {
                    return (DatePicker) child;
                } else if (child instanceof ViewGroup) {
                    DatePicker result = findDatePicker((ViewGroup) child);
                    if (result != null)
                        return result;
                }
            }
        }
        return null;
    }

    protected ProgressDialog mDialog;

    protected void queryLockedUsers(final int fromActivity) {
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        params.addBodyParameter("token", getToken());
        params.addBodyParameter("imei", getIMEI());
        switch (fromActivity) {
            case FROM_UNLOCK_ACTIVITY:
            case FROM_MAIN_ACTIVITY:
                params.addBodyParameter("type", RoleType.USER);
                break;
            case FROM_AUDIT_LIST_ACTIVITY:

                break;
        }
        String url = ConfigProperties.SERVICE_URL + "queryLockedUsers.do";
        Loger.e("url is " + url + " params is " + params);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                if (mDialog == null) {
                    mDialog = new ProgressDialog(BaseActivity.this);
                }
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("加载中,请等待...");
                mDialog.show();
                if (mUserList == null) {
                    mUserList = new ArrayList<MapEntity>();
                } else {
                    mUserList.clear();
                }
                if (fromActivity == FROM_AUDIT_LIST_ACTIVITY) {
                    queryLockedUsersResult();
                }
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (BaseActivity.this != null && !BaseActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        Loger.e("stringResponseInfo.result is " + stringResponseInfo.result);
                        try {
                            JSONObject jsonObject = new JSONObject(stringResponseInfo.result);
                            if (jsonObject != null) {
                                boolean success = jsonObject.getBoolean("success");
                                if (success) {
                                    parseUser(jsonObject);
                                    queryLockedUsersResult();
                                    return;
                                } else {
                                    String msg = jsonObject.getString("message");
                                    new ViewHelper(BaseActivity.this).showBTNDialog(msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        new ViewHelper(BaseActivity.this).showBTNDialog("加载失败,请重试!");
                    }
                    if (fromActivity == FROM_AUDIT_LIST_ACTIVITY) {
                        queryLockedUsersResult();
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Loger.e("msg:" + msg);
                if (BaseActivity.this != null && !BaseActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    new ViewHelper(BaseActivity.this).showErrorDialog(error);
                }
            }
        });

    }

    protected ArrayList<MapEntity> mUserList;

    protected void parseUser(JSONObject jsonObject) throws JSONException {
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        int length = jsonArray.length();
        for (int i = 0; i < length; i++) {
            JSONObject jsonObject2 = jsonArray.getJSONObject(i);
            MapEntity mapEntity = parseSingleUser(jsonObject2);
            mUserList.add(mapEntity);
        }
    }

    protected MapEntity parseSingleUser(JSONObject jsonObject2) throws JSONException {
        MapEntity mapEntity = new MapEntity();
        if (jsonObject2.has(AuditListResult.CHECKED_STR)) {
            mapEntity.setValue(AuditListResult.CHECKED, jsonObject2.getString(AuditListResult.CHECKED_STR));
        }
        if (jsonObject2.has(AuditListResult.CREATE_TIME_STR)) {
            mapEntity.setValue(AuditListResult.CREATE_TIME, jsonObject2.getString(AuditListResult.CREATE_TIME_STR));
        }
        mapEntity.setValue(AuditListResult.ERROR_TIMES, jsonObject2.getString(AuditListResult.ERROR_TIMES_STR));
        mapEntity.setValue(AuditListResult.FIRST_LOGIN, jsonObject2.getString(AuditListResult.FIRST_LOGIN_STR));
        mapEntity.setValue(AuditListResult.ID, jsonObject2.getString(AuditListResult.ID_STR));
        mapEntity.setValue(AuditListResult.LOCKED, jsonObject2.getString(AuditListResult.LOCKED_STR));
        mapEntity.setValue(AuditListResult.LOCKED_TIME, jsonObject2.getString(AuditListResult.LOCKED_TIME_STR));
        mapEntity.setValue(AuditListResult.LOGIN_NAME, jsonObject2.getString(AuditListResult.LOGIN_NAME_STR));
        mapEntity.setValue(AuditListResult.LOGIN_TIME, jsonObject2.getString(AuditListResult.LOCKED_TIME_STR));
        mapEntity.setValue(AuditListResult.MAC, jsonObject2.getString(AuditListResult.MAC_STR));
        mapEntity.setValue(AuditListResult.PASSWORD, jsonObject2.getString(AuditListResult.PASSWORD_STR));
        mapEntity.setValue(AuditListResult.QX1, jsonObject2.getString(AuditListResult.QX1_STR));
        mapEntity.setValue(AuditListResult.QX2, jsonObject2.getString(AuditListResult.QX2_STR));
        mapEntity.setValue(AuditListResult.QX3, jsonObject2.getString(AuditListResult.QX3_STR));
        mapEntity.setValue(AuditListResult.QX4, jsonObject2.getString(AuditListResult.QX4_STR));
        mapEntity.setValue(AuditListResult.QX5, jsonObject2.getString(AuditListResult.QX5_STR));
        mapEntity.setValue(AuditListResult.ROLE_ID, jsonObject2.getString(AuditListResult.ROLE_ID_STR));
        mapEntity.setValue(AuditListResult.UUID, jsonObject2.getString(AuditListResult.UUID_STR));
        return mapEntity;
    }

    protected void queryLockedUsersResult() {

    }

    protected void updateUnLockedResult() {

    }

    protected void updateUnLocked(final MapEntity mapEntity) {
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        String imei = getIMEI();
        params.addBodyParameter("token", getToken());
        params.addBodyParameter("imei", imei);
        params.addBodyParameter("id", mapEntity.getString(AuditListResult.ID));
        String url = ConfigProperties.SERVICE_URL + "updateUnLocked.do";
        Loger.e("url is " + url + " params is " + params);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                if (mDialog == null) {
                    mDialog = new ProgressDialog(BaseActivity.this);
                }
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("加载中,请等待...");
                mDialog.show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (BaseActivity.this != null && !BaseActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        Loger.e("stringResponseInfo.result is " + stringResponseInfo.result);
                        try {
                            JSONObject jsonObject = new JSONObject(stringResponseInfo.result);
                            if (jsonObject != null) {
                                boolean success = jsonObject.getBoolean("success");
                                if (success) {
                                    updateUnLockedResult();
                                } else {
                                    String msg = jsonObject.getString("message");
                                    new ViewHelper(BaseActivity.this).showBTNDialog(msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        new ViewHelper(BaseActivity.this).showBTNDialog("加载失败,请重试!");
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Loger.e("msg:" + msg);
                if (BaseActivity.this != null && !BaseActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    new ViewHelper(BaseActivity.this).showErrorDialog(error);
                }
            }
        });
    }

    protected void login(final int fromActivity, String username, String password) {
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        String imei = getIMEI();
        params.addBodyParameter("username", username);
        params.addBodyParameter("password", password);
        params.addBodyParameter("imei", imei);
        if (fromActivity == FROM_CLIENT_LOGIN_ACTIVITY) {
            params.addBodyParameter("type", RoleType.USER);
        }
        String url = ConfigProperties.SERVICE_URL + "login.do";
        Loger.e("url is " + url + " params is " + params);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                if (mDialog == null) {
                    mDialog = new ProgressDialog(BaseActivity.this);
                }
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("登录中,请等待...");
                mDialog.show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (BaseActivity.this != null && !BaseActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        Loger.e("stringResponseInfo.result is " + stringResponseInfo.result);
                        try {
                            JSONObject jsonObject = new JSONObject(stringResponseInfo.result);
                            if (jsonObject != null) {
                                boolean success = jsonObject.getBoolean("success");
                                if (success) {
                                    parseLoginResult(jsonObject);
                                } else {
                                    String msg = jsonObject.getString("message");
                                    new ViewHelper(BaseActivity.this).showBTNDialog(msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        new ViewHelper(BaseActivity.this).showBTNDialog("登陆失败,请重试!");
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Loger.e("msg:" + msg);
                if (BaseActivity.this != null && !BaseActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    new ViewHelper(BaseActivity.this).showErrorDialog(error);
                }
            }
        });

    }

    protected void logout() {
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        String imei = getIMEI();
        params.addBodyParameter("token", getToken());
        params.addBodyParameter("imei", imei);
        String url = ConfigProperties.SERVICE_URL + "logout.do";
        Loger.e("url is " + url + " params is " + params);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                if (mDialog == null) {
                    mDialog = new ProgressDialog(BaseActivity.this);
                }
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("退出中,请等待...");
                mDialog.show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (BaseActivity.this != null && !BaseActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        Loger.e("stringResponseInfo.result is " + stringResponseInfo.result);
                        try {
                            JSONObject jsonObject = new JSONObject(stringResponseInfo.result);
                            if (jsonObject != null) {
                                boolean success = jsonObject.getBoolean("success");
                                if (success) {
                                    // mSharePrefsHelper.setBoolean("isAuto",
                                    // false);
                                    finish();
                                } else {
                                    String msg = jsonObject.getString("message");
                                    new ViewHelper(BaseActivity.this).showBTNDialog(msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        new ViewHelper(BaseActivity.this).showBTNDialog("退出失败,请重试!");
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Loger.e("msg:" + msg);
                if (BaseActivity.this != null && !BaseActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    new ViewHelper(BaseActivity.this).showErrorDialog(error);
                }
            }
        });

    }

    protected void parseLoginResult(JSONObject jsonObject) throws JSONException {

    }

    protected void queryMembersByType(final int fromActivty) {
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        String imei = getIMEI();
        params.addBodyParameter("token", getToken());
        params.addBodyParameter("imei", imei);
        if (fromActivty == FROM_CHECK_LIST_ACTIVITY) {
            params.addBodyParameter("type1", RoleType.MANAGER);
            params.addBodyParameter("type2", RoleType.CHECKER);
            params.addBodyParameter("type3", RoleType.OPREATER);
        } else if (fromActivty == FROM_QUERY_DETAIL_LIST_ACTIVITY || fromActivty == FROM_MODIFY_DETAIL_ACTIVITY) {
            params.addBodyParameter("type1", RoleType.USER);
        }
        String url = ConfigProperties.SERVICE_URL + "queryMembersByType.do";
        Loger.e("url is " + url + " params is " + params);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                if (mDialog == null) {
                    mDialog = new ProgressDialog(BaseActivity.this);
                }
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("加载中,请等待...");
                mDialog.show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (BaseActivity.this != null && !BaseActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        Loger.e("stringResponseInfo.result is " + stringResponseInfo.result);
                        try {
                            JSONObject jsonObject = new JSONObject(stringResponseInfo.result);
                            if (jsonObject != null) {
                                boolean success = jsonObject.getBoolean("success");
                                if (success) {
                                    parseQueryMembersByTypeResult(fromActivty, jsonObject);
                                } else {
                                    String msg = jsonObject.getString("message");
                                    new ViewHelper(BaseActivity.this).showBTNDialog(msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        new ViewHelper(BaseActivity.this).showBTNDialog("加载失败,请重试!");
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Loger.e("msg:" + msg);
                if (BaseActivity.this != null && !BaseActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    new ViewHelper(BaseActivity.this).showErrorDialog(error);
                }
            }
        });

    }

    private void parseQueryMembersByTypeResult(int fromActivity, JSONObject jsonObject) throws JSONException {
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        int length = jsonArray.length();
        if (fromActivity == FROM_MODIFY_DETAIL_ACTIVITY) {
            mUserStrs = new String[length];
            for (int i = 0; i < length; i++) {
                JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                mUserStrs[i] = jsonObject2.getString(AuditListResult.LOGIN_NAME_STR);
            }
        } else {
            mUserStrs = new String[length + 1];
            mUserStrs[0] = "全部";
            for (int i = 0; i < length; i++) {
                JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                mUserStrs[i + 1] = jsonObject2.getString(AuditListResult.LOGIN_NAME_STR);
            }
        }
        if (mUserStrs != null && mUserStrs.length > 1) {
            parseQueryMembersByTypeResult();
        } else {
            if (fromActivity == FROM_MODIFY_DETAIL_ACTIVITY) {
                ToastHelper.getInstance().showShortMsg("没有更多用户可以选择");
            }
        }
    }

    protected void parseQueryMembersByTypeResult() {

    }

    protected void queryNewInfo(final int fromActivity, String type) {
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        params.addBodyParameter("token", getToken());
        params.addBodyParameter("imei", getIMEI());
        params.addBodyParameter("type", type);
        String url = ConfigProperties.SERVICE_URL + "queryNewInfo.do";
        Loger.e("url is " + url + " params is " + params);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                mDialog = new ProgressDialog(BaseActivity.this);
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("加载中,请等待...");
                mDialog.show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (BaseActivity.this != null && !BaseActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        Loger.e("stringResponseInfo.result is " + stringResponseInfo.result);
                        try {
                            JSONObject jsonObject = new JSONObject(stringResponseInfo.result);
                            if (jsonObject != null) {
                                boolean success = jsonObject.getBoolean("success");
                                if (success) {
                                    MapEntity entity = parseQueryNewInfoResult(jsonObject);
                                    parseQueryNewInfoResult(fromActivity, entity);
                                } else {
                                    String msg = jsonObject.getString("message");
                                    if (fromActivity != FROM_SERVER_MAIN_PAY_ACTIVITY
                                            && fromActivity != FROM_SERVER_MAIN_TOP_UP_ACTIVITY) {
                                        ToastHelper.getInstance().showShortMsg(msg);
                                        finish();
                                    } else {
                                        new ViewHelper(BaseActivity.this).showBTNDialog(msg);

                                    }
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        if (fromActivity != FROM_SERVER_MAIN_PAY_ACTIVITY
                                && fromActivity != FROM_SERVER_MAIN_TOP_UP_ACTIVITY) {
                            ToastHelper.getInstance().showShortMsg("加载失败,退出当前界面!");
                            finish();
                        } else {
                            new ViewHelper(BaseActivity.this).showBTNDialog("加载失败,请重试!");

                        }
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Loger.e("msg:" + msg);
                if (BaseActivity.this != null && !BaseActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (fromActivity != FROM_SERVER_MAIN_PAY_ACTIVITY
                            && fromActivity != FROM_SERVER_MAIN_TOP_UP_ACTIVITY) {
                        ToastHelper.getInstance().showShortMsg(getString(R.string.comm_no_internet));
                        finish();
                    } else {
                        new ViewHelper(BaseActivity.this).showErrorDialog(error);
                    }
                }
            }
        });

    }

    protected void parseQueryNewInfoResult(int fromActivity, MapEntity entity) {

    }

    protected MapEntity parseQueryNewInfoResult(JSONObject jsonObject) throws JSONException {
        JSONArray listArray = jsonObject.getJSONArray("list");
        MapEntity mapEntity = new MapEntity();
        int listlength = listArray.length();
        if (listlength > 0) {
            JSONObject object = listArray.getJSONObject(0);
            mapEntity.setValue(TradingEntity.ID, object.getString("id"));
            mapEntity.setValue(TradingEntity.MONEY, object.getString("money"));
            mapEntity.setValue(TradingEntity.STATUS, object.getString("status"));
            mapEntity.setValue(TradingEntity.TYPE, object.getString("type"));
            String yue = jsonObject.getString("yue");
            mapEntity.setValue(TradingEntity.YUE, yue);
        } else {
            return null;
        }
        return mapEntity;
    }

    protected void queryRecordsAdm(final int fromActivity, String type) {
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        params.addBodyParameter("token", getToken());
        params.addBodyParameter("imei", getIMEI());
        params.addBodyParameter("type", type);
        params.addBodyParameter("status", "0");
        String url = ConfigProperties.SERVICE_URL + "queryRecordsAdm.do";
        Loger.e("url is " + url + " params is " + params);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                mDialog = new ProgressDialog(BaseActivity.this);
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("加载中,请等待...");
                mDialog.show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (BaseActivity.this != null && !BaseActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        Loger.e("stringResponseInfo.result is " + stringResponseInfo.result);
                        try {
                            JSONObject jsonObject = new JSONObject(stringResponseInfo.result);
                            if (jsonObject != null) {
                                boolean success = jsonObject.getBoolean("success");
                                if (success) {
                                    MapEntity entity = parseQueryRecordsAdmResult(jsonObject);
                                    parseQueryRecordsAdmResult(fromActivity, entity);
                                } else {
                                    String msg = jsonObject.getString("message");
                                    if (fromActivity != FROM_SERVER_MAIN_PAY_ACTIVITY
                                            && fromActivity != FROM_SERVER_MAIN_TOP_UP_ACTIVITY) {
                                        ToastHelper.getInstance().showShortMsg(msg);
                                        finish();
                                    } else {
                                        new ViewHelper(BaseActivity.this).showBTNDialog(msg);
                                    }
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        if (fromActivity != FROM_SERVER_MAIN_PAY_ACTIVITY
                                && fromActivity != FROM_SERVER_MAIN_TOP_UP_ACTIVITY) {
                            ToastHelper.getInstance().showShortMsg("加载失败,退出当前界面!");
                            finish();
                        } else {
                            new ViewHelper(BaseActivity.this).showBTNDialog("加载失败,请重试!");

                        }
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Loger.e("msg:" + msg);
                if (BaseActivity.this != null && !BaseActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (fromActivity != FROM_SERVER_MAIN_PAY_ACTIVITY
                            && fromActivity != FROM_SERVER_MAIN_TOP_UP_ACTIVITY) {
                        ToastHelper.getInstance().showShortMsg(msg);
                        finish();
                    } else {
                        new ViewHelper(BaseActivity.this).showErrorDialog(error);
                    }
                }
            }
        });

    }

    protected void parseQueryRecordsAdmResult(int fromActivity, MapEntity entity) {

    }

    protected MapEntity parseQueryRecordsAdmResult(JSONObject jsonObject) throws JSONException {
        JSONArray listArray = jsonObject.getJSONArray("result");
        MapEntity mapEntity = new MapEntity();
        int listlength = listArray.length();
        if (listlength > 0) {
            JSONObject object = listArray.getJSONObject(0);
            mapEntity.setValue(QueryDetailResult.COMPLETE_TIME, object.getString(QueryDetailResult.COMPLETE_TIME_STR));
            mapEntity.setValue(QueryDetailResult.CREATE_TIME, object.getString(QueryDetailResult.CREATE_TIME_STR));
            mapEntity.setValue(QueryDetailResult.EXT_CNAME, object.getString(QueryDetailResult.EXT_CNAME_STR));
            mapEntity.setValue(QueryDetailResult.EXT_USER, object.getString(QueryDetailResult.EXT_USER_STR));
            mapEntity.setValue(QueryDetailResult.ID, object.getString(QueryDetailResult.ID_STR));
            mapEntity.setValue(QueryDetailResult.MONEY, object.getString(QueryDetailResult.MONEY_STR));
            mapEntity.setValue(QueryDetailResult.STATUS, object.getString(QueryDetailResult.STATUS_STR));
            mapEntity.setValue(QueryDetailResult.TYPE, object.getString(QueryDetailResult.TYPE_STR));
            mapEntity.setValue(QueryDetailResult.USER_NAME, object.getString(QueryDetailResult.USER_NAME_STR));
        } else {
            return null;
        }
        return mapEntity;
    }

    protected boolean isServiceRun(Context context, String serviceAction) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningServiceInfo> list = am.getRunningServices(30);
        for (RunningServiceInfo info : list) {
            String serviceClass = info.service.getClassName();
            if (TextUtils.equals(serviceAction, serviceClass)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getCanonicalName());
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getCanonicalName());
        MobclickAgent.onPause(this);
    }
}
